package com.czl.module_base.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

/**
 * @author by xubin,
 * @date on 2022/2/25.
 * PS: Not easy to write code, please indicate.
 */
public class SearchEditText extends AppCompatEditText {
    private boolean isHasFocus;
    private OnSearchListener mOnSearchListener;
    private boolean mAfterCloseFocus;

    public boolean isHasFocus() {
        return isHasFocus;
    }

    public void setHasFocus(boolean hasFocus) {
        isHasFocus = hasFocus;
    }

    public SearchEditText(@NonNull Context context) {
        super(context);
        init();
    }

    public SearchEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SearchEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //getCompoundDrawables:
        //Returns drawables for the left, top, right, and bottom borders.
        Drawable[] drawables = this.getCompoundDrawables();
        //取得right位置的Drawable
        //即我们在布局文件中设置的android:drawableRight
        //设置焦点变化的监听
        this.setOnFocusChangeListener(new FocusChangeListenerImpl());
        //设置EditText文字变化的监听
        this.addTextChangedListener(new TextWatcherImpl());
    }

    /**
     * 当手指抬起的位置在clean的图标的区域
     * 我们将此视为进行清除操作
     * getWidth():得到控件的宽度
     * event.getX():抬起时的坐标(改坐标是相对于控件本身而言的)
     * getTotalPaddingRight():clean的图标左边缘至控件右边缘的距离
     * getPaddingRight():clean的图标右边缘至控件右边缘的距离
     * 于是:
     * getWidth() - getTotalPaddingRight()表示:
     * 控件左边到clean的图标左边缘的区域
     * getWidth() - getPaddingRight()表示:
     * 控件左边到clean的图标右边缘的区域
     * 所以这两者之间的区域刚好是clean的图标的区域
     */
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            boolean isSearch = (event.getX() > (getWidth() - getTotalPaddingRight())) &&
                    (event.getX() < (getWidth() - getPaddingRight()));
            if (isSearch) {
                if (mAfterCloseFocus) {
                    clearFocus();
                }
                if (mOnSearchListener != null && getText() != null) {
                    mOnSearchListener.onSearch(getText().toString());
                }
            }
        }
        return super.onTouchEvent(event);
    }

    public OnSearchListener getOnSearchListener() {
        return mOnSearchListener;
    }

    public void setOnSearchListener(OnSearchListener onSearchListener) {
        mOnSearchListener = onSearchListener;
    }

    public void setAfterCloseFocus(boolean afterCloseFocus) {
        mAfterCloseFocus = afterCloseFocus;
    }

    private static class FocusChangeListenerImpl implements OnFocusChangeListener {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
        }

    }

    /**
     * 当输入结束后判断是否显示右边clean的图标
     */
    private static class TextWatcherImpl implements TextWatcher {
        @Override
        public void afterTextChanged(Editable s) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

    }

    public interface OnSearchListener {
        /**
         * 搜索
         *
         * @param s
         */
        void onSearch(String s);
    }
}
